﻿using System.Net;
using System.Text;

namespace SkyCli
{
    public class WebRequestWrapper
    {
        private readonly IWebProxy proxy;

        public WebRequestWrapper()
        {
            proxy = WebRequest.GetSystemWebProxy();
            proxy.Credentials = CredentialCache.DefaultCredentials;
        }

        public WebResponse Get(string url)
        {
            var client = WebRequest.Create(url);
            client.Credentials = CredentialCache.DefaultCredentials;
            client.Proxy = proxy;
            var response = client.GetResponse();
            return response;
        }

        public WebResponse Post(string url, string postData)
        {
            var client = WebRequest.Create(url);
            client.Credentials = CredentialCache.DefaultCredentials;
            client.Proxy = proxy;
            client.ContentType = "application/x-www-form-urlencoded";
            client.Method = "post";
            var bytes = Encoding.UTF8.GetBytes(postData);
            client.ContentLength = bytes.Length;
            using (var stream = client.GetRequestStream())
            {
                stream.Write(bytes, 0, bytes.Length);
                stream.Close();
            }
            var response = client.GetResponse();
            return response;
        }
    }
}
